<?php
/**
 * 支付宝wap手机支付控制器
 */
namespace app\admin\controller;
use Think\Controller;

class Payalipayweb extends Base
{

    public $api_url = 'https://mapi.alipay.com/gateway.do?';


    /*public $ALIPAY_PAYKEY = 'u1778kjbn6pa01ietcruwcw2y8o8qfa7';//合伙伙伴密匙
    public $partner = '2088231661760910';//支付宝合作者身份ID 2088开头*/

    public $ALIPAY_PAYKEY ='uxidc38s2u53vbfbp7xhu7c327wd62aw';//合伙伙伴密匙
    public $partner = '2088531794171741';//支付宝合作者身份ID 2088开头

    public function __construct()
    {
        parent::__construct();
        //  $this->ALIPAY_PAYKEY = C('ALIPAY_PAYKEY'); //合伙伙伴密匙
        //  $this->partner =  C('ALIPAY_PARTNER'); //支付宝合作者身份ID 2088开头
    }

    public function wappay($data){
        $data['service'] = 'create_direct_pay_by_user';
        $data['partner'] = $this->partner;//支付宝合作者身份ID 2088开头
        $data['_input_charset'] = 'utf-8';
        $data['sign_type'] = 'MD5';//DSA、RSA、MD5三个值可选，必须大写。

        $data['seller_id'] = $data['partner'];//卖家支付宝账号

        $data['payment_type'] = '1';//1商品购买

        $data['sign'] = $this->_getSign($data);
        $params = http_build_query($data);

        $api_url = $this->api_url.$params;
        //echo $api_url;die;
        return $api_url;

    }


    /**
     * 	作用：生成签名
     */
    protected function _getSign($Obj)
    {
        //过滤参数
        $Obj = $this->_paraFilter($Obj);

        foreach ($Obj as $k => $v)
        {
            $Parameters[$k] = $v;
        }
        //签名步骤一：按字典序排序参数
        ksort($Parameters);

        $String = $this->_createLinkstring($Parameters);

        //echo '【string1】'.$String.'</br>';
        //签名步骤二：在string后加入KEY

        $String = $String.$this->ALIPAY_PAYKEY;
        //echo "【string2】".$String."</br>";
        //签名步骤三：MD5加密

        $String = md5($String);

        return $String;
    }

    /**
     * 除去数组中的空值和签名参数
     * @param $para 签名参数组
     * return 去掉空值与签名参数后的新签名参数组
     */
    protected function _paraFilter($para) {
        $para_filter = array();

        foreach ($para as $key=>$val){
            if($key == "sign" || $key == "sign_type" || $val == "")continue;
            else	$para_filter[$key] = $para[$key];
        }
        /*while (list ($key, $val) = each($para)) {
            if($key == "sign" || $key == "sign_type" || $val == "")continue;
            else	$para_filter[$key] = $para[$key];
        }*/
        return $para_filter;
    }


    /**
     * 把数组所有元素，按照“参数=参数值”的模式用“&”字符拼接成字符串
     * @param $para 需要拼接的数组
     * return 拼接完成以后的字符串
     */
    protected function _createLinkstring($para) {
        $arg  = "";
        foreach ($para as $key=>$val){
            $arg.=$key."=".$val."&";
        }
        //去掉最后一个&字符
        $arg = substr($arg,0,strlen($arg)-1);
        //如果存在转义字符，那么去掉转义
        if(get_magic_quotes_gpc()){$arg = stripslashes($arg);}

        return $arg;
    }


    /*
     * 验证回调的真实性
     */
    public function checkSign($data){

        if(empty($data)) {//判断POST来的数组是否为空
            return false;
        }

        $sign = $this->_getSign($data);
        $post_sign = $data["sign"];
        if($sign == $post_sign){
            return true;
        }

        return false;
    }


}
